//
//  RSDetailView.m
//  SanYiTeaching
//
//  Created by 李文强 on 2019/4/12.
//  Copyright © 2019 李文强. All rights reserved.
//

#import "RSDetailView.h"
#import "SYRequestAPI+Teach.h"
#import "StudentInformationObject.h"


@implementation RSDetailView

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/
- (instancetype)initWithFrame:(CGRect)frame addClassID:(NSString *)classID addPaperID:(NSString *)paperID andPaperQuestionId:(NSString *)paperQuestionId{
    if ([super initWithFrame:frame]) {
        self.classId = classID;
        self.paperId = paperID;
        self.paperQuestionId = paperQuestionId;
        [self initSubView];
        [self getShiTiXiangQingRequest];
        
    }
    return self;
}
- (void)initSubView{
    _studentsArray = [NSMutableArray new];
    self.backgroundColor = CONTENTBACKCOLOR;
    [self addSubview:self.backButton];
    [_backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@10);
        make.top.equalTo(@10);
        make.width.equalTo(@50);
        make.height.equalTo(@20);
    }];
    [self addSubview:self.titleLabel];
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.equalTo(@100);
        make.height.equalTo(@15);
        make.centerX.equalTo(self.mas_centerX);
        make.centerY.equalTo(self.backButton.mas_centerY);
    }];
    
    [self addSubview:self.tableList];
    [_tableList mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@0);
        make.right.equalTo(@0);
        make.top.equalTo(@44);
        make.bottom.equalTo(@0).offset(-20);
    }];
}
- (UIButton *)backButton{
    if (!_backButton) {
        UIButton *sender = [UIButton buttonWithType:UIButtonTypeCustom];
        [sender setTitle:@" 返回" forState:UIControlStateNormal];
        [sender setTitleColor:[UIColorChange colorwithHexString:@"#20B097"] forState:UIControlStateNormal];
        [sender setImage:[UIImage imageNamed:@"back_color"] forState:UIControlStateNormal];
        sender.titleLabel.font = [UIFont systemFontOfSize:12];
        [sender addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];
        _backButton = sender;
    }
    return _backButton;
}
- (UILabel *)titleLabel{
    if (!_titleLabel) {
        UILabel *label = [[UILabel alloc] init];
        label.frame = CGRectMake(618,34.5,63,15);
        label.numberOfLines = 0;
        label.textAlignment = 1;
        NSMutableAttributedString *string = [[NSMutableAttributedString alloc] initWithString:@"试题详情" attributes:@{NSFontAttributeName: [UIFont fontWithName:@"PingFang-SC-Regular" size: 16],NSForegroundColorAttributeName: [UIColor colorWithRed:51/255.0 green:51/255.0 blue:51/255.0 alpha:1.0]}];
        label.attributedText = string;
        _titleLabel = label;
    }
    return _titleLabel;
}
- (void)backClick:(UIButton *)sender{
    [UIView animateWithDuration:0.5 animations:^{
        self.frame = CGRectMake(APP_WIDTH, 0, MAINW, self.frame.size.height);
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
- (UITableView *)tableList{
    if (!_tableList) {
        UITableView *tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 0) style:UITableViewStyleGrouped];
        tableView.delegate  =self;
        tableView.dataSource = self;
        [tableView registerClass:[RSDCell class] forCellReuseIdentifier:@"RSDCell"];
        [tableView registerClass:[RSDCNextCell class] forCellReuseIdentifier:@"RSDCNextCell"];
        [tableView registerClass:[ZuoCuoXueShengCell class] forCellReuseIdentifier:@"ZuoCuoXueShengCell"];
        tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableList = tableView;
    }
    return _tableList;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 3) {
         NSLog(@"学生数 %d",_studentsArray.count);
        int num = _studentsArray.count%5;
        if (num>0) {
            return _studentsArray.count/5+1;
        }else{
            return _studentsArray.count/5;
        }
       
    }
    return 1;
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 40;
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 2) {
        return 150;
    }else if (indexPath.section == 1){
        return 50;
    }else if (indexPath.section == 3){
        return 50;
    }
    return 150;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 2) {
        RSDCNextCell *cell2 = [tableView dequeueReusableCellWithIdentifier:@"RSDCNextCell" forIndexPath:indexPath];
        if (!cell2) {
            cell2 = [[RSDCNextCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RSDCNextCell"];
        }
        [cell2.webView loadHTMLString:[NSString stringWithFormat:@"%@",_mainModel.questionAnalysis] baseURL:nil];
        cell2.shiTiLabel.text = [NSString stringWithFormat:@"试题:%@",_mainModel.questionType];
        cell2.duiLabel.text = [NSString stringWithFormat:@"做对:%@",_mainModel.rightStudents];
        cell2.cuoLabel.text = [NSString stringWithFormat:@"做错:%@",_mainModel.wrongStudents];
        cell2.zhengQueLabel.text = [NSString stringWithFormat:@"正确率:%@%%",_mainModel.rightRatio];
        cell2.biaoQianLabel.text = [NSString stringWithFormat:@"标签:%@",_mainModel.wrongReason];
        cell2.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell2;
    }else if(indexPath.section == 3){
         ZuoCuoXueShengCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ZuoCuoXueShengCell" forIndexPath:indexPath];
        if (!cell) {
            cell = [[ZuoCuoXueShengCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"ZuoCuoXueShengCell"];
        }
        NSMutableArray *arr = [self.studentsDIC objectForKey: [NSString stringWithFormat:@"%d",(int)indexPath.row]];
        cell.dataArray = arr;
        cell.backgroundColor = [UIColor whiteColor];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        [cell layoutView];
        return cell;
    }else{
        RSDCell *cell1 = [tableView dequeueReusableCellWithIdentifier:@"RSDCell" forIndexPath:indexPath];
        if (!cell1) {
            cell1 = [[RSDCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"RSDCell"];
        }
        if (indexPath.section == 0) {
            [cell1.webView loadHTMLString:[NSString stringWithFormat:@"%@",_mainModel.questionContent] baseURL:nil];
        }else if (indexPath.section == 1){
            [cell1.webView loadHTMLString:[NSString stringWithFormat:@"%@",_mainModel.questionAnswer] baseURL:nil];
        }else if (indexPath.section == 2){
            
        }
        cell1.backgroundColor = [UIColor whiteColor];
        cell1.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell1;
    }

}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *mainView = [[UIView alloc]init];
    mainView.backgroundColor = CONTENTBACKCOLOR;
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.frame.size.width, 40)];
    label.backgroundColor = CONTENTBACKCOLOR;
    label.textColor = TITLECOLOR;
    label.font = [UIFont systemFontOfSize:14];
    label.text = @" 变通";
    if (section == 0) {
        label.text = @" 题目";
    }else if (section == 1){
        label.text = @" 答案";
    }else if (section == 2){
        label.text = @" 解析";
    }else{
        label.text = @" 错题学生";
    }
    label.textAlignment = 0;
    [mainView addSubview:label];
    return mainView;
}
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *mainView = [[UIView alloc]init];
    return mainView;
}
#pragma mark - 试题详情
- (void)getShiTiXiangQingRequest{
    NSDictionary *parama = @{@"classId":self.classId,
                             @"paperId":self.paperId,
                             @"paperQuestionId":self.paperQuestionId};
    [MBProgressHUD mj_showMessage:nil toView:self];
    [SYRequestAPI sy_getClassServiceCountByQuestionDetail:parama success:^(id  _Nonnull responseObject) {
//        YLSLog(@"输出试题详情 %@",responseObject);
        self->_mainModel = [ShiTiXiangQingModel yy_modelWithJSON:[responseObject objectForKey:@"data"]];
        self->_mainModel.dataStudentModel = [NSMutableArray new];
        NSMutableArray *array = self->_mainModel.dataStudent;
        for (NSDictionary *dic in array) {
            StudentInformationObject *info = [StudentInformationObject yy_modelWithJSON:dic];
            [self->_studentsArray addObject:info];
        }
//        YLSLog(@"学生数  %@",self->_studentsArray);
        self->_studentsDIC = [self chuLiShuZu:self->_studentsArray];
        [self.tableList reloadData];
        [MBProgressHUD mj_hideHUDForView:self];
    } fail:^(NSError *error) {
        [MBProgressHUD mj_hideHUDForView:self];
    }];
}
- (NSMutableDictionary *)chuLiShuZu:(NSMutableArray *)array{
    NSMutableDictionary *dic = [NSMutableDictionary new];
    int fiveCount = (int)array.count/5;
    int yuShu = array.count%5;
    for (int j =0;j<fiveCount;j++) {
        NSMutableArray *resultArray = [NSMutableArray new];
        for (int i = 0; i<5; i++) {
            [resultArray addObject:array[i+(j*5)]];
        }
        [dic setValue:resultArray forKey:[NSString stringWithFormat:@"%d",j]];
    }
    NSMutableArray *resultArray = [NSMutableArray new];
    for (int i = 0; i<yuShu; i++) {
        [resultArray addObject:array[array.count-1-i]];
    }
    [dic setValue:resultArray forKey:[NSString stringWithFormat:@"%d",fiveCount]];
    NSLog(@"试题详情 %@",dic);
    return dic;
}
//- (void)getZuoTiXueShengRequest{
//    NSDictionary *parama = @{@"templateQuestionId":_mainModel.questionId,
//                             @"studentsIds":self.classId};
//    [SYRequestAPI sy_getClassServiceAnsweredQuestionDetail:parama success:^(id  _Nonnull responseObject) {
//        NSLog(@"输出做题的人 %@",responseObject);
//    } fail:^(NSError *error) {
//
//    }];
//}
@end
